package com.wellink.android_demo.com.wellink.android_demo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

public class CounterViewer extends View {

    private Paint paint;
    private Rect rect;
    private int count = 0;

    public CounterViewer(Context context, AttributeSet attrs) {
        super(context, attrs);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        rect = new Rect();

        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                count ++;

                invalidate();
            }
        });
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(Color.BLACK);
        canvas.drawRect(0,0,getWidth(),getHeight(),paint);

        paint.setColor(Color.YELLOW);
        paint.setTextSize(50);
        String text = String.valueOf(this.count);

        paint.getTextBounds(text,0,text.length(),rect);

        float textWidth = rect.width();
        float textHeight = rect.height();

        canvas.drawText(text,getWidth()/2-textWidth/2,getHeight()/2-textHeight/2,paint);
    }

}
